#include "ShaderManager.h"

ShaderManager::ShaderManager(CGcontext context, CGprofile profile)
{
	this ->context = context;
	this ->profile = profile;
}

ShaderManager::~ShaderManager(void)
{
}

void ShaderManager::Load(const char *path, const char *ext)
{
	std::string searchPath = path;
	searchPath += "\\*.";
	searchPath += ext;

	WIN32_FIND_DATA fd;

	HANDLE hfile = FindFirstFile(searchPath.c_str(), &fd);
	if (hfile != INVALID_HANDLE_VALUE)
	{
		BOOL doSearch = true;
		while (doSearch)
		{
			std::string shaderPath = path;
			shaderPath += "\\";
			shaderPath += fd.cFileName;
			Shader *shader = new Shader(context, profile, shaderPath.c_str());

			std::string shaderFileName = fd.cFileName;
			Add(shaderFileName.substr(0, shaderFileName.size() - 4).c_str(), shader);

			doSearch = FindNextFile(hfile, &fd);
		}

		FindClose(hfile);
	}
}

void ShaderManager::Add(const char *name, Shader *res)
{
	shaders[name] = res;
}

Shader * ShaderManager::Get(const char *name)
{
	std::map<std::string, Shader*>::iterator i = shaders.find(name);
	if (i == shaders.end())
		return NULL;

	return (*i).second;
}

int ShaderManager::GetCount()
{
	return (int)shaders.size();
}

void ShaderManager::Get(int i, char *name, Shader *&res)
{
	std::map<std::string, Shader*>::iterator it = shaders.begin();
	while (i--)
		it ++;

	strcpy(name, (*it).first.c_str());
	res = (*it).second;
}
